package photobox.manage

import grails.transaction.Transactional
import photobox.account.PublicizeImg
import photobox.utils.HQLUtils

@Transactional
class PublicizeImgService {

    def list(searchParam, pageParam, sortParam) {
        def hql = ""
        hql += " from PublicizeImg A where 1=1 "
        def params = [:]
        if (searchParam.id) {
            hql += " and " + HQLUtils.whereCondition("A.id", searchParam.id.opt, "id")
            params.id = searchParam.id.data
        }
        if (searchParam.name) {
            hql += " and " + HQLUtils.whereCondition("A.name", searchParam.name.opt, "name")
            params.name = searchParam.name.data
        }
        if (searchParam.photoPath) {
            hql += " and " + HQLUtils.whereCondition("A.photoPath", searchParam.photoPath.opt, "photoPath")
            params.photoPath = searchParam.photoPath.data
        }
        if (searchParam.linkurl) {
            hql += " and " + HQLUtils.whereCondition("A.linkurl", searchParam.linkurl.opt, "linkurl")
            params.linkurl = searchParam.linkurl.data
        }
//        if (searchParam.location) {
//            hql += " and " + HQLUtils.whereCondition("A.location", searchParam.location.opt, "location")
//            params.location = searchParam.location.data
//        }
        if (searchParam.isShow) {
            hql += " and " + HQLUtils.whereCondition("A.isShow", searchParam.isShow.opt, "isShow")
            params.isShow = searchParam.isShow.data
        }


        def sortHql = ""
        if (sortParam.sort) {
            sortHql = " order by A.${sortParam.sort} ${sortParam.order} "
        }
        def results = PublicizeImg.executeQuery("select distinct A " + hql + sortHql, params,
                [max: pageParam.pageSize, offset: pageParam.offset])
        def count = PublicizeImg.executeQuery("select count(distinct A.id) " + hql, params)
        return [rs:results, total:count[0]]
    }
    def createPublicizeImg(params) {
        def publicizeImg = new PublicizeImg()
        publicizeImg.name = params.name
        publicizeImg.linkurl = params.linkurl
//        publicizeImg.location = params.location
        publicizeImg.photoPath = params.photoPath
       if(params.isShow =="on") {
           publicizeImg.isShow = publicizeImg.IS_SHOW_YES
       }else{
           publicizeImg.isShow = publicizeImg.IS_SHOW_NO
       }
        if(!publicizeImg.save()){
            println(publicizeImg.errors)
        }

    }
    def updatePublicizeImg(params){
        def publicizeImg = PublicizeImg.get(params.id)
        def oldPath = publicizeImg.photoPath

        publicizeImg.name = params.name
        publicizeImg.linkurl = params.linkurl
//        publicizeImg.location = params.location
        publicizeImg.photoPath = params.photoPath
        if(params.isShow=="0") {
            publicizeImg.isShow = publicizeImg.IS_SHOW_YES
        }else if(params.isShow==null){
            publicizeImg.isShow = publicizeImg.IS_SHOW_NO
        }
        if(!publicizeImg.save()){
            println(publicizeImg.errors)
        }
        File f = new File("web-app\\"+oldPath)
     println(oldPath)
        println(f)
        f.delete()
    }
    def deletePublicizeImg(params){
        def publicizeImg = PublicizeImg.get(params.id)
        publicizeImg.delete()
    }
}
